package com.wander.rep.server;

import java.util.LinkedList;
import java.util.List;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public final class CommonUtilities {

    /**
     * Base URL of the Demo Server (such as http://my_host:8080/gcm-demo)
     */
    public static final String DEFAULT_SERVER_URL = "http://192.168.1.100:8080/WanderRepServer";
    public static final String FEEDBACK_URL = DEFAULT_SERVER_URL + "/feedback";

    /**
     * Google API project id registered to use GCM.
     */
    public static final String SENDER_ID = "512464838757";

    /**
     * Tag used on log messages.
     */
    static final String TAG = "GCMDemo";

    /**
     * Intent used to display a message in the screen.
     */
    public static final String DISPLAY_MESSAGE_ACTION = "com.google.android.gcm.demo.app.DISPLAY_MESSAGE";

    /*
     * Intent's extra containing type of content to be passed in GCM Message
     * 0. Notification
     * 1. Status
     * 2. Activity Level
     * 3. Temperature History
     * 4. Movement Pattern
     */
    
    public static final String EXTRA_CONTENT_TYPE = "content_type";
    /*
     * Intent's extra that contains the message's title to be displayed
     */
    public static final String EXTRA_TITLE = "title";
    
    /**
     * Intent's extra that contains the message to be displayed.
     */
    public static final String EXTRA_MESSAGE = "message";
	private static String PREF_SERVER_URL;

    /**
     * Notifies UI to display a message.
     * <p>
     * This method is defined in the common helper because it's used both by
     * the UI and the background service.
     *
     * @param context application's context.
     * @param message message to be displayed.
     */
   public static void displayMessage(Context context, String message) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtra(EXTRA_MESSAGE, message);
        context.sendBroadcast(intent);
    }
   
   /**
    * 
    * Gets Server IP from SharedPreferences
    * as set by user in the App's Settings Activity
    * 
    */
   
   @SuppressWarnings("unused")
private static void setServerIP(Context context)
   {
	   String serverIPKey = "Server IP Address";
	   SharedPreferences sp = context.getSharedPreferences("com.wander.rep", Context.MODE_PRIVATE);
	   
	   PREF_SERVER_URL = sp.getString(serverIPKey, DEFAULT_SERVER_URL);
   }
   
   public static String getUsername(Context context)
   {
		AccountManager manager = AccountManager.get(context); 
		Account[] accounts = manager.getAccountsByType("com.google"); 
		List<String> possibleEmails = new LinkedList<String>();

		for (Account account : accounts) {
			// TODO: Check possibleEmail against an email regex or treat
			// account.name as an email address only for certain account.type values.
			possibleEmails.add(account.name);
		}

		if(!possibleEmails.isEmpty() && possibleEmails.get(0) != null)
		{
			String email = possibleEmails.get(0);
			String[] parts = email.split("@");
			if(parts.length > 0 && parts[0] != null)
				return parts[0];
			else
				return null;
		}
		else
			return null;
	}
}